package com.ruoyi.test.controller;


import com.ruoyi.test.service.impl.DriverGeoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;


@RestController
@RequestMapping("/driver")
public class DriverController {

    @Autowired
    private DriverGeoService driverGeoService;

    @PostMapping("/user/location")
    public String addUserLocation(@RequestParam String userId,
                                  @RequestParam double longitude,
                                  @RequestParam double latitude,
                                  @RequestParam String locationName) {
        driverGeoService.addUserLocation(userId, longitude, latitude, locationName);
        return "OK";
    }

    @GetMapping("/distance/points")
    public ResponseEntity<?> getDistanceBetweenPoints(@RequestParam String location1,
                                                      @RequestParam String location2) {
        try {
            double distance = driverGeoService.getDistanceBetweenPoints(location1, location2);
            if (distance == -1) {
                Map<String, Object> response = new HashMap<>();
                response.put("msg", "地点不存在");
                response.put("code", 404);
                return ResponseEntity.status(HttpStatus.NOT_FOUND).body(response);
            }

            Map<String, Object> response = new HashMap<>();
            response.put("distance", distance+"米");
            return ResponseEntity.ok(response);
        } catch (RuntimeException e) {
            Map<String, Object> response = new HashMap<>();
            response.put("msg", e.getMessage());
            response.put("code", 400);
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(response);
        }
    }
}